#!/usr/bin/env python3
"""
plsm 基本使用示例
"""

import sys
import os

# 添加项目根目录到Python路径
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))

from plsm import ServiceManager, ServiceConfig


def main():
    """基本使用示例"""
    
    # 创建服务管理器实例（需要sudo权限）
    manager = ServiceManager(sudo=True)
    
    # 列出所有服务
    print("=== 列出所有服务 ===")
    services = manager.list_all_services()
    for service in services[:5]:  # 只显示前5个
        print(f"{service.name}: {service.status.value}")
    
    # 检查特定服务状态
    print("\n=== 检查nginx服务状态 ===")
    nginx_info = manager.get_service_info("nginx")
    if nginx_info:
        print(f"服务名: {nginx_info.name}")
        print(f"状态: {nginx_info.status.value}")
        print(f"是否运行: {nginx_info.running}")
        print(f"描述: {nginx_info.description}")
    else:
        print("nginx服务未找到")
    
    # 创建示例服务配置
    print("\n=== 创建示例服务配置 ===")
    config = ServiceConfig(
        name="my-custom-service",
        description="My Custom Service created by plsm",
        exec_start="/usr/bin/python3 -m http.server 8080",
        working_directory="/tmp",
        user="nobody",
        restart="on-failure"
    )
    
    # 验证服务配置
    print("\n=== 验证服务配置 ===")
    validation = manager.validate_service("nginx")
    for key, value in validation.items():
        print(f"{key}: {value}")
    
    # 获取运行中的服务
    print("\n=== 运行中的服务 ===")
    running_services = manager.get_running_services()
    for service in running_services[:3]:  # 只显示前3个
        print(f"{service.name}: {service.description}")
    
    # 获取失败的服务
    print("\n=== 失败的服务 ===")
    failed_services = manager.get_failed_services()
    if failed_services:
        for service in failed_services:
            print(f"{service.name}: {service.description}")
    else:
        print("没有失败的服务")


if __name__ == "__main__":
    main()